package com.nny.alarm.alert;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Random;

import android.app.ActionBar.LayoutParams;
import android.content.res.Resources;
import android.os.SystemClock;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.nny.alarm.R;
import com.nny.alarm.more.AlarmBriefUtil;
import com.uxwine.alarm.Alarm;
import com.uxwine.alarm.AlarmActivity;
import com.uxwine.alarm.AlarmCalc;
import com.uxwine.util.UxToast;
import com.uxwine.view.DoubleClick;

public class AlertActivity extends AlarmActivity implements View.OnClickListener
{
	AlarmBriefUtil mAUtil;
	ImageView[] mivNNY;
	int[] mnImageIds;
	Button mbtnSnooze;
	Animation mAnimation;
	DoubleClick mDoubleClick;
	
	static final int TYPE_NAO 	= 0;
	static final int TYPE_NA	= 1;
	static final int TYPE_YANG 	= 2;

	@Override
	protected void onSnoozeEnabled(boolean isEnabled)
	{
		//mbtnSnooze.setEnabled(isEnabled);
	}

	@Override
	protected boolean isSnoozeEnable()
	{
		return true;//mbtnSnooze.isEnabled();
	}

	@Override
	protected void onUpdateLayout(Alarm alarm)
	{
		setContentView(R.layout.activity_alert);
		mDoubleClick = new DoubleClick();
		
		mAUtil = new AlarmBriefUtil(this);
		
		TextView tvTime = (TextView)findViewById(R.id.alert_tv_time);
		TextView tvCycle = (TextView)findViewById(R.id.alert_tv_cycle);
		TextView tvBrief = (TextView)findViewById(R.id.alert_tv_brief);
		
		tvBrief.setText(alarm.label);		
		Calendar trigger = AlarmCalc.triggerTime(alarm.time, alarm.cycle);
		if(alarm.cycle.isOnce())
		{
			trigger.setTimeInMillis(alarm.trigger);
		}
		String brief = mAUtil.getDetail(alarm.cycle, trigger);
		tvTime.setText(brief);
		tvCycle.setText(mAUtil.getCycleString(alarm.cycle));
		
		mnImageIds = new int[]
		{
			R.drawable.alert_btn_nao,
			R.drawable.alert_btn_na,
			R.drawable.alert_btn_yang
		};
				
		mAnimation = AnimationUtils.loadAnimation(this, R.anim.alert_btn);
		mAnimation.setRepeatCount(2);
		initNNY(getResources(), getPackageName());
	}

	@Override
	protected void onSnoozed(Alarm alarm, long time)
	{
		UxToast.sshow(this, "displayTime");
	}

	@Override
	public boolean onTouchEvent(MotionEvent event)
	{
		if(event.getAction() == MotionEvent.ACTION_DOWN)
		{
			if(mDoubleClick.trace())
				snooze();
		}
		return super.onTouchEvent(event);
	}
	
	
	

	@Override
	public void onClick(View v)
	{
		Object tag = (Object)v.getTag();
		if(null != tag && tag instanceof Integer)
		{
			int index = (Integer)tag;
			updateImages(index);
		}
	}
	
	protected void updateImages(int type)
	{
		ImageView iv = mivNNY[type];
		if(type == 2)
		{
			if(iv.isSelected())
			{				
				UxToast.sshow(this, "error");							
			}
			else
			{
				for(int i = type - 1; i >= 0; i--)
				{
					ImageView prev = mivNNY[i];
					if(prev.isSelected() && (type - 1) == i)
					{
						dismiss(false);
						return;
					}
					if(prev.isSelected())
					{
						prev = mivNNY[i + 1];
						prev.startAnimation(mAnimation);
						return;
					}
				}
				mivNNY[0].startAnimation(mAnimation);
			}
		}
		else if(type == 1)
		{
			if(iv.isSelected())
			{
				ImageView next = mivNNY[type + 1];
				if(next.isSelected())
					next.setSelected(false);
				next.startAnimation(mAnimation);
			}
			else
			{
				ImageView prev = mivNNY[type - 1];
				if(prev.isSelected())
				{
					iv.setSelected(true);
					mivNNY[type + 1].startAnimation(mAnimation);
				}
				else
				{
					prev.startAnimation(mAnimation);
				}
			}
		}
		else if(type == 0)
		{
			if(iv.isSelected())
			{
				boolean isSelected = false;
				for(int i = type + 1; i < 3; i++)
				{
					ImageView next = mivNNY[i];
					if(!isSelected && !next.isSelected())
					{
						next.startAnimation(mAnimation);
						isSelected = true;
					}
				}				
			}
			else
			{
				iv.setSelected(true);
				ImageView next = mivNNY[type + 1];
				next.startAnimation(mAnimation);
			}
		}
	}
	
	protected void initNNY(final Resources res, final String defPackage)
	{
		final Random r = new Random(System.currentTimeMillis());
		List<Integer> seeds = new ArrayList<Integer>();
		seeds.add(TYPE_NAO);seeds.add(TYPE_NA);seeds.add(TYPE_YANG);
		
		
		int count = seeds.size();
		int[] types = new int[count];
		for(int m = 0, n = count; m < 3; m++,n--)
		{
			int index = Math.abs(r.nextInt()) % n;
			types[m] = seeds.get(index);
			seeds.remove(index);
		}		
		
		mivNNY = new ImageView[count];
		for(int i = 0; i < count; i++)
		{
			final int id = res.getIdentifier("alert_iv_nny" + i, "id", defPackage);
			ImageView iv = (ImageView)findViewById(id);
			mivNNY[types[i]] = iv;
			initImages(iv, types[i]);
		}
	}
	
	protected void initImages(ImageView iv, int type)
	{
		iv.setImageResource(mnImageIds[type]);
		iv.setClickable(true);
		iv.setOnClickListener(this);
		iv.setTag((Integer)type);
	}
}
